package com.server.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class InsertData {
	public static InsertData instance;
	public static InsertData GetInstance(){
		if(instance == null){
			instance = new InsertData();
		}
		return instance;
	}

	/**
	 * 插入新玩家
	 * @throws SQLException 
	 */
	public long InsertNewPlayer(long appid,String unionid) throws SQLException {
		long uid = -4;
  		Connection conn = DBPool.getConnection();
    	String sql = "insert into users (unionid,appid) values(?,?)";
    	PreparedStatement pstmt = null;
    	try {
			pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			pstmt.setString(1, unionid); 
			pstmt.setLong(2, appid);
        	pstmt.executeUpdate();
        	ResultSet rs = pstmt.getGeneratedKeys();
        	while(rs.next()) {
        		uid = rs.getLong(1);
        	}
        	rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
			uid = -3;
		}
    	pstmt.close();
        conn.commit();
        conn.close();
  		return uid;
	}
	
	/**
     * 添加管理员
     * @param username
     * @param password
     * @param email
     * @return
     * @throws SQLException
     */
    public boolean AddAccount(String username, String password, int power) throws SQLException{
    	boolean isSuccess = false;
    	String sql = "insert into account (uname,password,power) values(?,?,?)"; 
    	Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try{
    		pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);  
            pstmt.setString(2, password);  
            pstmt.setInt(3, power);
            pstmt.executeUpdate();
            isSuccess = true;
    	}
    	catch(Exception e){
    		e.printStackTrace();
        	isSuccess = false;
        }
    	
        pstmt.close();
        conn.commit();
        conn.close();
        return isSuccess;
    }
    
    /**
     * 添加应用
     * @param appname
     * @param appkey
     * @return
     * @throws SQLException
     */
    public boolean AddApp(long appid, String appname, String appkey) throws SQLException{
    	boolean isSuccess = false;
    	String sql = "insert into app (id,appname,appkey) values(?,?,?)"; 
    	Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try{
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setLong(1, appid);
            pstmt.setString(2, appname);  
            pstmt.setString(3, appkey);
            pstmt.executeUpdate();
            isSuccess = true;
    	}
    	catch(Exception e){
    		e.printStackTrace();
        	isSuccess = false;
        }
        pstmt.close();
        conn.commit();
        conn.close();
        return isSuccess;
    }
    
    /**
     * 添加应用开关
     * @param appname
     * @param appkey
     * @return
     * @throws SQLException
     */
    public boolean AddAppSwitch(long appid, String appname) throws SQLException{
    	boolean isSuccess = false;
    	String sql = "insert into app_switch (id,appname) values(?,?)"; 
    	Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try{
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setLong(1, appid);
            pstmt.setString(2, appname);
            pstmt.executeUpdate();
            isSuccess = true;
    	}
    	catch(Exception e){
    		e.printStackTrace();
        	isSuccess = false;
        }
        pstmt.close();
        conn.commit();
        conn.close();
        return isSuccess;
    }
    
    /**
	 * 插入存档数据
	 */
	public int InsertGameData(long uid ,long appid, String key,String value) throws SQLException{
		int state = 0;
		String sql = "insert into data (appid,datakey,uid,value) values(?,?,?,?)";
		Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setLong(1, appid);
    		pstmt.setString(2, key);
    		pstmt.setLong(3, uid);
    		pstmt.setString(4, value);
    		pstmt.executeUpdate();
	        state = 1;
		} catch (SQLException e) {
			e.printStackTrace();
			state = -3;
		}
    	pstmt.close();
    	conn.commit();
    	conn.close();
    	return state;
	}
	
	/**
 	 * 建立新的排行榜表单
 	 * @param tablename 表名
 	 * @return
 	 * @throws SQLException
 	 */
	public int CreateRankListTable(String tablename) throws SQLException{
		int state = 0;
		String sql = "CREATE TABLE `userverdb`.`" + tablename + "` (`uid` BIGINT(13) UNSIGNED NOT NULL,`uname` VARCHAR(60) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL,`uhead` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_bin' NULL DEFAULT NULL,`value` BIGINT(13) NOT NULL DEFAULT '0',`info` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_bin' NULL DEFAULT NULL,PRIMARY KEY (`uid`),UNIQUE INDEX `uid_UNIQUE` (`uid` ASC),INDEX `value` (`value` DESC));";
		Connection conn = DBPool.getConnection();
    	Statement stmt = null;
    	try {
    		stmt = conn.createStatement();
    		stmt.execute(sql);
	        state = 1;
		} catch (SQLException e) {
			e.printStackTrace();
			state = -3;
		}
    	stmt.close();
    	conn.commit();
    	conn.close();
    	return state;
	}
	
	/**
	 * 添加排行榜
	 * @param appid
	 * @param appname
	 * @param rkname
	 * @param resettime
	 * @return
	 * @throws SQLException
	 */
    public int AddRankList(long appid, String appname, String rkname,int resettime) throws SQLException{
    	int id = 0;
    	String sql = "insert into rklist (appid,appname,rkname,reset_time) values(?,?,?,?)"; 
    	Connection conn = DBPool.getConnection();
    	PreparedStatement pstmt = null;
    	try{
    		pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
    		pstmt.setLong(1, appid);
            pstmt.setString(2, appname);  
            pstmt.setString(3, rkname);
            pstmt.setInt(4, resettime);
            pstmt.executeUpdate();
            ResultSet rs = pstmt.getGeneratedKeys();
        	while(rs.next()) {
        		id = rs.getInt(1);
        	}
        	rs.close();
    	}
    	catch(Exception e){
    		e.printStackTrace();
        }
        pstmt.close();
        conn.commit();
        conn.close();
        return id;
    }
    
    /**
   	 * 插入已使用礼包码
   	 */
   	public void UseRedeemCode(String redeem) throws SQLException{
   		String sql = "insert into rcdata (redeem,value) values(?,?)";
   		Connection conn = DBPool.getConnection();
       	PreparedStatement pstmt = null;
       	try {
       		pstmt = conn.prepareStatement(sql);
       		pstmt.setString(1, redeem);
       		pstmt.setInt(2, 1);
            pstmt.executeUpdate();
   		} catch (SQLException e) {
   			e.printStackTrace();
   		}
       	pstmt.close();
       	conn.commit();
       	conn.close();
   	}
   	
   	/**
   	 * 插入已使用名字
   	 */
   	public void AddName(String pname) throws SQLException{
   		String sql = "insert into pname (pname) values(?)";
   		Connection conn = DBPool.getConnection();
       	PreparedStatement pstmt = null;
       	try {
       		pstmt = conn.prepareStatement(sql);
       		pstmt.setString(1, pname);
            pstmt.executeUpdate();
   		} catch (SQLException e) {
   			e.printStackTrace();
   		}
       	pstmt.close();
       	conn.commit();
       	conn.close();
   	}
   	
   	/**
   	 * 插入已使用名字
   	 */
   	public boolean BindUsename(String uname, String pword, Long uid) throws SQLException{
   		boolean state = false;
   		String sql = "insert into username (uid, username, password) values(?,?,?)";
   		Connection conn = DBPool.getConnection();
       	PreparedStatement pstmt = null;
       	try {
       		pstmt = conn.prepareStatement(sql);
       		pstmt.setLong(1, uid);
       		pstmt.setString(2, uname);
       		pstmt.setString(3, pword);
            pstmt.executeUpdate();
            state = true;
   		} catch (SQLException e) {
   			e.printStackTrace();
   		}
       	pstmt.close();
       	conn.commit();
       	conn.close();
       	return state;
   	}
}
